#ifndef __OPENGLRENDERERLINUX_H__
#define __OPENGLRENDERERLINUX_H__

bool OpenGLRenderer::InitAPI(bool vSync)
{
/* 
// screen = DefaultScreen(display);

// int nModes;
// XF86VidModeGetAllModeLines(display, screen, &nModes, &dmodes);

// Array <DispRes> modes;

  char str[64];
  int foundMode = -1;
  for (int i = 0; i < nModes; i++){
    if (dmodes[i]->hdisplay >= 640 && dmodes[i]->vdisplay >= 480){
      modes.add(newRes(dmodes[i]->hdisplay, dmodes[i]->vdisplay, i));

      if (dmodes[i]->hdisplay == fullscreenWidth && dmodes[i]->vdisplay == fullscreenHeight){
        foundMode = i;
      }
    }
  }

  resolution->clear();
  modes.sort(dComp);
  for (uint i = 0; i < modes.getCount(); i++){
    sprintf(str, "%dx%d", modes[i].w, modes[i].h);
    int index = resolution->addItemUnique(str);
    if (modes[i].index == foundMode) resolution->selectItem(index);
  }


  if (fullscreen){
    if (foundMode >= 0 && XF86VidModeSwitchToMode(display, screen, dmodes[foundMode])){
      XF86VidModeSetViewPort(display, screen, 0, 0);
    } else {
      char str[128];
      sprintf(str, "Couldn't set fullscreen at %dx%d.", fullscreenWidth, fullscreenHeight);
      ErrorMsg(str);
      fullscreen = false;
    }
  }

  XVisualInfo *vi;
  while (true){
    int attribs[] = {
      GLX_RGBA,
      GLX_DOUBLEBUFFER,
      GLX_RED_SIZE,      8,
      GLX_GREEN_SIZE,    8,
      GLX_BLUE_SIZE,     8,
      GLX_ALPHA_SIZE,    (colorBits > 24)? 8 : 0,
      GLX_DEPTH_SIZE,    depthBits,
      GLX_STENCIL_SIZE,  stencilBits,
      GLX_SAMPLE_BUFFERS_ARB, (antiAliasSamples > 0),
      GLX_SAMPLES_ARB,         antiAliasSamples,
      None,
    };

    vi = glXChooseVisual(display, screen, attribs);
    if (vi != NULL) break;

    antiAliasSamples -= 2;
    if (antiAliasSamples < 0){
      char str[256];
      sprintf(str, "No Visual matching colorBits=%d, depthBits=%d and stencilBits=%d", colorBits, depthBits, stencilBits);
      ErrorMsg(str);
      return false;
    }
  }


  //printf("Selected visual = 0x%x\n",(unsigned int) (vi->visualid));
  glContext = glXCreateContext(display, vi, None, True);


  XSetWindowAttributes attr;
  attr.colormap = XCreateColormap(display, RootWindow(display, screen), vi->visual, AllocNone);

  attr.border_pixel = 0;
  attr.override_redirect = fullscreen;
    attr.event_mask = ExposureMask | KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask |
    PointerMotionMask | StructureNotifyMask;

  window = XCreateWindow(display, RootWindow(display, vi->screen),
      0, 0, width, height, 0, vi->depth, InputOutput, vi->visual,
      CWBorderPixel | CWColormap | CWEventMask | CWOverrideRedirect, &attr);

  if (!fullscreen){
      Atom wmDelete;
        wmDelete = XInternAtom(display, "WM_DELETE_WINDOW", True);
        XSetWMProtocols(display, window, &wmDelete, 1);
    char *title = "OpenGL";
        XSetStandardProperties(display, window, title, title, None, NULL, 0, NULL);
  }
    XMapRaised(display, window);

  // Create a blank cursor for cursor hiding
  XColor dummy;
  char data = 0;
  Pixmap blank = XCreateBitmapFromData(display, window, &data, 1, 1);
  blankCursor = XCreatePixmapCursor(display, blank, blank, &dummy, &dummy, 0, 0);
  XFreePixmap(display, blank);


  XGrabKeyboard(display, window, True, GrabModeAsync, GrabModeAsync, CurrentTime);


  glXMakeCurrent(display, window, glContext);

  initExtensions(display);

  if (antiAliasSamples > 0){
    glEnable(GL_MULTISAMPLE_ARB);
  }

  if (fullscreen) captureMouse(!configDialog->isVisible());

  renderer = new OpenGLRenderer(window, glContext, display, screen);
  renderer->setViewport(width, height);

  antiAlias->selectItem(antiAliasSamples / 2);

  linearClamp = renderer->addSamplerState(LINEAR, CLAMP, CLAMP, CLAMP);
  defaultFont = renderer->addFont("../Textures/Fonts/Future.dds", "../Textures/Fonts/Future.font", linearClamp);
  blendSrcAlpha = renderer->addBlendState(SRC_ALPHA, ONE_MINUS_SRC_ALPHA);
  noDepthTest  = renderer->addDepthState(false, false);
  noDepthWrite = renderer->addDepthState(true,  false);
  cullNone  = renderer->addRasterizerState(CULL_NONE);
  cullBack  = renderer->addRasterizerState(CULL_BACK);
  cullFront = renderer->addRasterizerState(CULL_FRONT); */ 
  return true;
  
}

void OpenGLRenderer::ExitAPI()
{
  /*
//  delete renderer;

    glXMakeCurrent(display, None, NULL);
    glXDestroyContext(display, glContext);

	if (fullscreen){
		if (XF86VidModeSwitchToMode(display, screen, dmodes[0])){
			XF86VidModeSetViewPort(display, screen, 0, 0);
		}
	}
    XFree(dmodes);
	XFreeCursor(display, blankCursor);

	XDestroyWindow(display, window);

	XSync(display, False);

   */    
}

#endif /* _OPENGLRENDERERLINUX_H_ */
